package com.agw.crm.workbench.web.controller;

import com.agw.crm.settings.domain.User;
import com.agw.crm.settings.service.UserService;
import com.agw.crm.settings.service.impl.UserServiceImpl;
import com.agw.crm.utils.*;
import com.agw.crm.vo.PaginationVo;
import com.agw.crm.workbench.domain.Activity;
import com.agw.crm.workbench.domain.ActivityRemark;
import com.agw.crm.workbench.service.ActivityService;
import com.agw.crm.workbench.service.impl.ActivityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进入到市场活动控制器
        System.out.println("进入到市场活动控制器");
        //获取请求路径，根据请求路径调方法
        String path = request.getServletPath();

        if ("/workbench/activity/getUserList.do".equals(path)){
            getUserList(request,response);
        }else if ("/workbench/activity/save.do".equals(path)){
            save(request,response);
        }else if ("/workbench/activity/pageList.do".equals(path)){
            pageList(request,response);
        }else if ("/workbench/activity/delete.do".equals(path)){
            delete(request,response);
        }else if ("/workbench/activity/getUserListAndActivity.do".equals(path)){
            getUserListAndActivity(request,response);
        }else if ("/workbench/activity/update.do".equals(path)){
            update(request,response);
        }else if ("/workbench/activity/detail.do".equals(path)){
            detail(request,response);
        }else if ("/workbench/activity/getRemarkListByAid.do".equals(path)){
            getRemarkListByAid(request,response);
        }else if ("/workbench/activity/deleteRemark.do".equals(path)){
            deleteRemark(request,response);
        }else if ("/workbench/activity/saveRemark.do".equals(path)){
            saveRemark(request,response);
        }else if ("/workbench/activity/updateRemark.do".equals(path)){
            updateRemark(request,response);
        }
    }

    private void updateRemark(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始修改备注信息");

        String id = request.getParameter("id");
        String noteContent = request.getParameter("noteContent");
        //获取到修改时间
        String editTime = DateTimeUtil.getSysTime();
        //获取到修改人
        String editBy = ((User)request.getSession().getAttribute("user")).getName();
        //更改是否修改的标志
        String editFlag = "1";

        //将数据封装到activityRemark对象中
        ActivityRemark ar = new ActivityRemark();
        ar.setId(id);
        ar.setEditFlag(editFlag);
        ar.setNoteContent(noteContent);
        ar.setEditBy(editBy);
        ar.setEditTime(editTime);

        //调用代理对象，处理业务
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag = as.updateRemark(ar);
        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        map.put("ar",ar);
        PrintJson.printJsonObj(response,map);

    }

    private void saveRemark(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("开始添加备注信息");
        //获取参数信息
        String noteContent = request.getParameter("noteContent");
        String activityId =  request.getParameter("activityId");
        String id = UUIDUtil.getUUID();
        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User)request.getSession().getAttribute("user")).getName();
        String editFlag = "0";

        //将数据封装到数据中
        ActivityRemark ar = new ActivityRemark();
        ar.setNoteContent(noteContent);
        ar.setActivityId(activityId);
        ar.setCreateTime(createTime);
        ar.setCreateBy(createBy);
        ar.setEditFlag(editFlag);
        ar.setId(id);

        //调用业务层处理业务
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag = as.saveRemark(ar);

        //将成功或者失败以及ar对象封装到map集合中
        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        map.put("ar",ar);

        PrintJson.printJsonObj(response,map);
    }

    private void deleteRemark(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("开始删除市场活动备注信息");
        String id = request.getParameter("id");

        //调用代理对象进行数据处理
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag = as.deleteRemark(id);
        PrintJson.printJsonFlag(response,flag);
    }

    private void getRemarkListByAid(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("开始查询市场活动备注信息");

        String activityId = request.getParameter("activityId");

        //调用代理对象，在业务层进行数据处理
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        List<ActivityRemark> list = as.getRemarkListByAid(activityId);

        PrintJson.printJsonObj(response,list);
    }

    private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("查询市场活动详细信息");
        //获取id
        String id = request.getParameter("id");

        //调用代理对象，在业务层进行数据处理
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity  activity = as.detail(id);

        //通过请求转发，将数据保存在request作用域中
        request.setAttribute("activity",activity );
        request.getRequestDispatcher("/workbench/activity/detail.jsp").forward(request,response);

    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("修改市场活动");
        //获取参数
        String id = request.getParameter("id");
        String owner = request.getParameter("owner");
        String name = request.getParameter("name");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String cost = request.getParameter("cost");
        String description = request.getParameter("description");

        //修改时间
        String editTime = DateTimeUtil.getSysTime();
        //修改人
        String editBy = ((User)request.getSession().getAttribute("user")).getName();

        //将参数封装到Activity对象中
        Activity activity = new Activity();
        activity.setId(id);
        activity.setOwner(owner);
        activity.setName(name);
        activity.setStartDate(startDate);
        activity.setEndDate(endDate);
        activity.setCost(cost);
        activity.setDescription(description);
        activity.setEditTime(editTime);
        activity.setEditBy(editBy);

        //使用代理对象
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag = as.update(activity);

        PrintJson.printJsonFlag(response,flag);
    }

    private void getUserListAndActivity(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始查询所有者信息和单条市场活动信息");

        //获取参数
        String id = request.getParameter("id");

        //调用代理类,在业务层处理
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        /*
            在这里使用map，因为，修改操作不常用，使用map就足够了
         */
        Map<String,Object> map = as.getUserListAndActivity(id);

        PrintJson.printJsonObj(response,map);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始删除市场活动");

        //获取参数
        String[] ids = request.getParameterValues("id");
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.delete(ids);

        PrintJson.printJsonFlag(response,flag);

    }



    private void pageList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始查询市场活动信息");

        //获取参数
        String pageNoStr = request.getParameter("pageNo");
        String pageSizeStr = request.getParameter("pageSize");
        String name = request.getParameter("name");
        String owner = request.getParameter("owner");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");


        int pageNo = Integer.valueOf(pageNoStr);//页码
        int pageSize = Integer.valueOf(pageSizeStr);//每页展现的记录数
        //把页码进行计算
        pageNo = (pageNo - 1) * pageSize;//获取到页数

        //将参数封装到map集合中
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);

        //使用动态代理在业务层进行处理
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        /*
            返回值根据前端需要要返回total，和list
            使用map
                map.put("total",total);
                map.put("dataList",dataList);
                最后将map转为json格式字符串

            使用volei
                PaginationVo<T>
                private int total;
                private List<T> dataList;

            这里要使用vo类，因为以后的代码要复用，泛型就起到了复用的作用


         */
        //这里将PaginationVo类返回
        PaginationVo<Activity> vo = as.pageList(map);

        PrintJson.printJsonObj(response,vo);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("添加市场活动");
        //获取参数
        String id = UUIDUtil.getUUID();
        String owner = request.getParameter("owner");
        String name = request.getParameter("name");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String cost = request.getParameter("cost");
        String description = request.getParameter("description");

        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User)request.getSession().getAttribute("user")).getName();

        //将参数封装到Activity对象中
        Activity activity = new Activity();
        activity.setId(id);
        activity.setOwner(owner);
        activity.setName(name);
        activity.setStartDate(startDate);
        activity.setEndDate(endDate);
        activity.setCost(cost);
        activity.setDescription(description);
        activity.setCreateTime(createTime);
        activity.setCreateBy(createBy);

        //使用代理对象
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag = as.save(activity);

        PrintJson.printJsonFlag(response,flag);

    }

    private void getUserList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("取得用户信息列表");
        //创建道理对象，在业务层进行业务处理
        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());
        //调用接口方法，进行业务处理
        List<User> userlist = us.getUserList();
        //调用方法，向前端传值
        PrintJson.printJsonObj(response,userlist);
    }


}
